package proyecto.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.sql.DataSource;

import proyecto.exception.DAOExcepcion;
import proyecto.modelo.Cartelera;
import proyecto.modelo.Categoria;
import proyecto.modelo.Cine;
import proyecto.modelo.Pelicula;
import proyecto.modelo.Sala;


public class CarteleraDAOImpl extends BaseDAO implements CarteleraDAO {

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public Collection<Cartelera> consulta_listado() throws DAOExcepcion {
		System.out.println("Llegaste aca??");
		Collection<Cartelera> c = new ArrayList<Cartelera>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			//select DoseMg from DrugDosing where Frequency = 'OD' and (DoseMg = 30 or DoseMg = 10);
			String query = "select in_IdCartelera,cine_in_codCine,sala_in_codSala,pelicula_in_codPelicula,ts_horaCartelera,ts_fechaCartelera from cartelera"; 
			
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cartelera vo = new Cartelera();
								
				vo.setCine(rs.getInt("cine_in_codCine"));
				vo.setSala(rs.getInt("sala_in_codSala"));
				vo.setPelicula(rs.getInt("pelicula_in_codPelicula"));
				vo.setTs_horaCartelera(rs.getDate("ts_horaCartelera"));
				vo.setTs_fechaCartelera(rs.getDate("ts_fechaCartelera"));
				
				c.add(vo);
				System.out.println("Registro: " + vo.getCine() + 
						 		   "," + vo.getSala() + 
						 		   "," + vo.getPelicula() +
						 		   "," + vo.getTs_horaCartelera() +
						 		   "," + vo.getTs_fechaCartelera());
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	
	

	@Override
	public Cartelera insertar(Cartelera vo) throws DAOExcepcion {
		// TODO Auto-generated method stub
		String query = "insert  into cartelera (cine_in_codCine,sala_in_codSala,pelicula_in_codPelicula,ts_horaCartelera,ts_fechaCartelera) values (?,?,?,?,?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getCine());
			stmt.setInt(2, vo.getSala());
			stmt.setInt(3, vo.getPelicula());
			stmt.setDate(4, (Date) vo.getTs_horaCartelera());
			stmt.setDate(5, (Date) vo.getTs_fechaCartelera());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			} // Obtener el ultimo id 
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIn_idCartelera(rs.getInt(1));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	@Override
	public Cartelera actualizar(Cartelera vo) throws DAOExcepcion {
		// TODO Auto-generated method stub
		
		String query = "update cartelera cine_in_codCine=?,sala_in_codSala=?,pelicula_in_codPelicula=?,ts_horaCartelera=?,ts_fechaCartelera=?) where ";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getCine());
			stmt.setInt(2, vo.getSala());
			stmt.setInt(3, vo.getPelicula());
			stmt.setDate(4, (Date) vo.getTs_horaCartelera());
			stmt.setDate(5, (Date) vo.getTs_fechaCartelera());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			} // Obtener el ultimo id 
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIn_idCartelera(rs.getInt(1));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
		
	}
	
}
